package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.example.mapper.CategoryBasicMapper;
import org.example.mapper.UserBasicMapper;
import org.example.model.category.CategoryBasic;
import org.example.model.common.dtos.PageResponseDto;
import org.example.model.common.dtos.category.CategoryBasicDto;
import org.example.service.CategoryBasicService;
import org.example.service.UserBasicService;
import org.example.utils.PageUtils;
import org.example.utils.UserIdThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类管理
 */
@Service
@Slf4j
@Transactional
public class CategoryBasicServiceImpl extends ServiceImpl<CategoryBasicMapper, CategoryBasic> implements CategoryBasicService {

    @Autowired
    private CategoryBasicMapper categoryBasicMapper;

    @Autowired
    private UserBasicService userBasicService;

    /**
     * 新增分类
     *
     * @param categoryBasic
     */
    @Override
    public Boolean InsertCategoryBasic(CategoryBasic categoryBasic) {
        //判断分类数据是否为空
        if (categoryBasic == null) return false;
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return false;
        //插入初始数据
        categoryBasic.setCreateUser(userId);
        categoryBasic.setCreateDate(LocalDateTime.now());
        categoryBasic.setUpdateUser(userId);
        categoryBasic.setUpdateDate(LocalDateTime.now());
        categoryBasic.setIsDeleted(false);
        return categoryBasicMapper.insert(categoryBasic) > 0;
    }

    /**
     * 根据id查询分类详情
     *
     * @param id
     */
    @Override
    public CategoryBasicDto SelectCategoryById(int id) {
        if (id <= 0) return new CategoryBasicDto();
        CategoryBasic categoryBasic = categoryBasicMapper.selectById(id);
        if (categoryBasic == null) return new CategoryBasicDto();
        CategoryBasicDto categoryBasicDto = new CategoryBasicDto();
        BeanUtils.copyProperties(categoryBasic,categoryBasicDto);
        int createId = categoryBasic.getCreateUser();
        int updateId = categoryBasic.getUpdateUser();
        categoryBasicDto.setCreateUser(userBasicService.SelectUserById(createId).getUsername());
        categoryBasicDto.setUpdateUser(userBasicService.SelectUserById(updateId).getUsername());
        return categoryBasicDto;
    }

    /**
     * 根据Id修改分类信息
     *
     * @param categoryBasic
     */
    @Override
    public Boolean UpdateCategoryBasicById(CategoryBasic categoryBasic) {
        if (categoryBasic == null) return false;
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return false;
        CategoryBasic category = categoryBasicMapper.selectById(categoryBasic.getId());
        if (category == null) return false;
        category.setUpdateUser(userId);
        category.setUpdateDate(LocalDateTime.now());
        category.setCategoryName(categoryBasic.getCategoryName());
        category.setSort(categoryBasic.getSort());
        category.setDescription(categoryBasic.getDescription());
        category.setIsDeleted(categoryBasic.isIsDeleted());
        return categoryBasicMapper.updateById(category) > 0;
    }

    /**
     * 根据Id删除分类信息
     *
     * @param id
     */
    @Override
    public Boolean DeleteCategoryBasicById(int id) {
        CategoryBasic categoryBasic = categoryBasicMapper.selectById(id);
        categoryBasic.setIsDeleted(true);
        return this.UpdateCategoryBasicById(categoryBasic);
    }

    /**
     * 按条件(分类名称模糊查询，分类类型精确查询)分页查询
     *
     * @param categoryBasic
     * @param pageIndex
     * @param pageSize
     */
    @Override
    public PageResponseDto<List<CategoryBasicDto>> SelectPageByConditions(CategoryBasic categoryBasic, int pageIndex, int pageSize) {
        LambdaQueryWrapper<CategoryBasic> queryWrapper = new LambdaQueryWrapper<>();
        //分类名称模糊查询，分类类型精确查询
        queryWrapper.eq(CategoryBasic::isIsDeleted,false)
                .like(StringUtils.isNotBlank(categoryBasic.getCategoryName()),CategoryBasic::getCategoryName,categoryBasic.getCategoryName())
                .eq(categoryBasic.getType() != 0,CategoryBasic::getType,categoryBasic.getType())
                .orderByDesc(CategoryBasic::getSort)
                .orderByDesc(CategoryBasic::getCreateDate);
        List<CategoryBasic> categoryBasics = categoryBasicMapper.selectList(queryWrapper);
        List<CategoryBasicDto> categoryBasicDtoList = new ArrayList<>();
        if (categoryBasics.size() == 0) return new PageResponseDto<List<CategoryBasicDto>>();
        CategoryBasicDto categoryBasicDto = null;
        for (int i = 0; i < categoryBasics.size(); i++) {
            categoryBasicDto = new CategoryBasicDto();
            BeanUtils.copyProperties(categoryBasics.get(i),categoryBasicDto);
            categoryBasicDtoList.add(categoryBasicDto);
        }
        categoryBasicDtoList = categoryBasicDtoList.stream().map(item -> {
            item = this.SelectCategoryById(item.getId());
            return item;
        }).collect(Collectors.toList());
        PageResponseDto<List<CategoryBasicDto>> pageResponseDto = new PageResponseDto<>();
        pageResponseDto.setSize(categoryBasicDtoList.size());
        pageResponseDto.setData(new PageUtils<CategoryBasicDto>().page(categoryBasicDtoList, pageIndex, pageSize));
        return pageResponseDto;
    }
}

